// pages/index/index.js
Page({
  data: {
    activeTab: 0,
    welcomeText: '欢迎回来',
    currentDistrict: '定位中...',
    liveList: [
      {
        id: 1,
        cover: '/static/images/live.png',
        title: '深夜食堂营业中',
        shop: '居酒屋·道',
        viewers: 1280
      },
      {
        id: 2,
        cover: '/static/images/live.png',
        title: '现烤面包房',
        shop: '麦香坊',
        viewers: 890
      }
    ],
    banners: [
      {id: 1, img: '/static/images/banner.png'},
      {id: 2, img: '/static/images/banner.png'}
    ],
    shopList: [
      {
        id: 1,
        logo: '/static/images/shop.png',
        name: '轻食沙拉',
        rating: 4.9,
        tags: ['健康餐', '减脂', '素食'],
        delivery: 3,
        sales: 1500
      },
      {
        id: 2,
        logo: '/static/images/shop.png',
        name: '奶茶工坊',
        rating: 4.8,
        tags: ['买一送一', '新品上市'],
        delivery: 0,
        sales: 2300
      }
    ]
  },

  onLoad() {
    this.getLocation()
  },

  // 获取定位
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: () => {
        // 模拟定位数据
        this.setData({
          currentDistrict: '杭州市·西湖区',
          welcomeText: '发现西湖美食'
        })
      },
      fail: () => {
        this.setData({
          currentDistrict: '位置获取失败',
          welcomeText: '欢迎使用'
        })
      }
    })
  },

  // 切换选项卡
  switchTab(e) {
    this.setData({
      activeTab: Number(e.currentTarget.dataset.tab)
    })
  },

  // 跳转搜索
  goSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },

  // 查看直播
  viewLive(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/live/live?id=${id}`
    })
  },

  // 查看商家
  viewShop(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/shop/shop?id=${id}`
    })
  }
})